#include "loggermanager.h"
#include <QHostAddress> //用于校验输入的Ip地址


LoggerManager* LoggerManager::m_instance = nullptr;

LoggerManager::LoggerManager(QObject *parent)
    : QObject(parent)
{
    AppenderFactory::instance();//初始化AppenderFactory的构造函数
}

LoggerManager* LoggerManager::instance() {
    if (!m_instance) {
        static QRecursiveMutex mutex;
        QMutexLocker locker(&mutex);
        if (!m_instance) {
            m_instance = new LoggerManager;
        }
    }
    return m_instance;
}

void LoggerManager::initConsoleAppender(const QString &appenderName, LogLevel level)
{
    auto factory = AppenderFactory::instance();
    factory->addAppender(appenderName, AppenderType::Console, {}, level);
}

void LoggerManager::initFileAppender(const QString &logPath, const QString &logName, const QString &appenderName, LogLevel level)
{
    auto factory = AppenderFactory::instance();
    AppenderParams fileParams;
    fileParams["path"] = logPath;
    fileParams["baseName"] = logName;
    factory->addAppender(appenderName, AppenderType::File, fileParams, level);
}


void LoggerManager::initQTextEditAppender(QTextEdit* textEdit,const QString &appenderName, LogLevel level)
{
    if(!textEdit)return;
    auto factory = AppenderFactory::instance();
    AppenderParams textParams;
    textParams["textEdit"] = QVariant::fromValue(textEdit); // 传递QTextEdit指针
    factory->addAppender(appenderName, AppenderType::QTextEdit, textParams, level);
}

void LoggerManager::initQPlainTextEditAppender(QPlainTextEdit* plainTextEdit,const QString &appenderName, LogLevel level)
{
    if(!plainTextEdit)return;
    auto factory = AppenderFactory::instance();
    AppenderParams plainTextParams;
    plainTextParams["plainTextEdit"] = QVariant::fromValue(plainTextEdit); // 传递QTextEdit指针
    factory->addAppender(appenderName, AppenderType::QPlainTextEdit, plainTextParams, level);
}

void LoggerManager::initTcpAppender(const QString &ip, quint16 port, const QString &appenderName, LogLevel level)
{
    if(!isIPv4Address(ip))return;
    auto factory = AppenderFactory::instance();
    AppenderParams tcpParams;
    tcpParams["ip"] = ip;
    tcpParams["port"] = port;
    factory->addAppender(appenderName, AppenderType::Tcp, tcpParams, level);
}

void LoggerManager::initUdpAppender(const QString &ip, quint16 port, const QString &appenderName, LogLevel level)
{
    if(!isIPv4Address(ip))return;
    auto factory = AppenderFactory::instance();
    AppenderParams udpParams;
    udpParams["ip"] = ip;
    udpParams["port"] = port;
    factory->addAppender(appenderName, AppenderType::Udp, udpParams, level);
}

QList<QString> LoggerManager::getAllAppenders()
{
    return AppenderFactory::instance()->getAllAppenders();
}

QList<QString> LoggerManager::getActiveAppenders()
{
    return AppenderFactory::instance()->getActiveAppenders();
}

QList<AppenderType> LoggerManager::getAllAppenderType()
{
    return AppenderFactory::instance()->getAllAppenderType();
}

QList<AppenderType> LoggerManager::getActiveAppenderType()
{
    return AppenderFactory::instance()->getActiveAppenderType();
}

void LoggerManager::setEnable(AppenderType type, bool enable)
{
    AppenderFactory::instance()->setAppenderEnabled(type, enable);
}

void LoggerManager::setEnable(const QString &appenderName, bool enable)
{
    AppenderFactory::instance()->setAppenderEnabled(appenderName, enable);
}

void LoggerManager::setAllEnable(bool enable)
{
    foreach (const QString& appenderName, getAllAppenders()) {
        setEnable(appenderName,enable);
    }
}

void LoggerManager::startLogSystem()
{
    AppenderFactory::instance()->startRedirect();
}

void LoggerManager::stopLogSystem()
{
    AppenderFactory::instance()->stopRedirect();
}

bool LoggerManager::isIPv4Address(const QString &ipStr)
{
    QHostAddress addr(ipStr);
    // QHostAddress::Invalid 表示地址无效，QHostAddress::IPv4Protocol 表示是IPv4
    return addr.protocol() == QHostAddress::IPv4Protocol;
}


